package com.anche.cywebnew.Controller.configManager;

import com.anche.cywebnew.Pojo.systemManager.SysResource;
import com.anche.cywebnew.Service.configManager.ResourceService;
import com.anche.cywebnew.Utils.ResponseBody.ResponseResult;
import com.anche.cywebnew.Utils.Shiro.ShiroRealm;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/config/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    private static final Logger logger = LoggerFactory.getLogger(ResourceController.class);


    @RequestMapping("/manager")
    public String toResourcePage() {
        return "configManager/resource";
    }

    @GetMapping("/resourceList")
    @ResponseBody
    public Object getResourceList() {
        try {
            logger.info("查询资源列表成功");
            List<SysResource> sysResourceList = resourceService.getResourceTable();
            return ResultUtil.success(sysResourceList);
        } catch (Exception e) {
            logger.error("<查询资源列表异常:[{}]>", e.getMessage());
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/resourceInfo", method = RequestMethod.POST)
    @ResponseBody
    @OptLog(title = "添加资源信息", type = OptType.ADD)
    public Object addResource(SysResource sysResource) {
        try {
            resourceService.addResource(sysResource);
            logger.info("<新增[{}]资源成功>", sysResource.getName());
            RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
            ShiroRealm realm = (ShiroRealm) rsm.getRealms().iterator().next();
            realm.clearAuthz();
            logger.info("刷新shiro缓存权限成功");
            return ResultUtil.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<新增[{}]资源异常>:{}", sysResource.getName(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/resourceInfo", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "更新资源信息", type = OptType.UPDATE)
    public Object setResource(SysResource sysResource) {
        try {
            resourceService.updateResource(sysResource);
            logger.info("<更新[{}]资源成功>", sysResource.getName());
            RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
            ShiroRealm realm = (ShiroRealm) rsm.getRealms().iterator().next();
            realm.clearAuthz();
            logger.info("刷新shiro缓存权限成功");
            return ResultUtil.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新[{}]资源异常>:{}", sysResource.getName(), e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/status", method = RequestMethod.PUT)
    @ResponseBody
    @OptLog(title = "添加资源状态信息", type = OptType.UPDATE)
    public Object setResourceStatus(@RequestParam("id") String id, @RequestParam("status") Integer status) {
        try {
            resourceService.updateStatusById(id, status);
            logger.error("<更新id为[{}]的资源状态成功>", id);
            return ResultUtil.success("状态更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("<更新id为[{}]的资源状态异常>:{}", id, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @RequestMapping(value = "/resourceInfo", method = RequestMethod.DELETE)
    @ResponseBody
    @OptLog(title = "删除资源信息", type = OptType.DEL)
    public ResponseResult delResource(@RequestParam("id") String id) {
        ResponseResult responseResult = new ResponseResult();
        try {
            resourceService.delResourceById(id);
            RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
            ShiroRealm realm = (ShiroRealm) rsm.getRealms().iterator().next();
            realm.clearAuthz();
            logger.info("刷新shiro缓存权限成功");
            responseResult.setCode("1");
            responseResult.setMessage("资源删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode("0");
            responseResult.setMessage(e.getMessage());
            logger.error("<删除id为 " + id + " 的资源异常>:{}", e);
        }
        return responseResult;
    }

    @GetMapping("/treeInfo")
    @ResponseBody
    public Object getTreeResource(HttpServletRequest request) {
        String type = request.getParameter("type");
        try {
            List<Object> list = resourceService.getTreeResource(type);
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

}
